/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.preferences;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;

/**
 *
 * @author error216
 */
public class Preferences {
    
    private static final Logger logger = Logger.getLogger(Preferences.class);
    
    private Map<String,String> initialValuesMap =
            new HashMap<String,String>();
    private Map<String,String> valuesMap = new HashMap<String,String>();
    private Set<Handler> handlers = new HashSet<Handler>();
    private Map<String,Handler> handlersMap = new HashMap<String,Handler>();
    
    public void add(String name, String initialValue, Handler handler) {
        initialValuesMap.put(name, initialValue);
        valuesMap.put(name, initialValue);
        handlers.add(handler);
        handlersMap.put(name, handler);
    }
    
    public void remove(String name) {
        valuesMap.remove(name);
        handlersMap.remove(name);
    }
    
    public void setValue(String name, String newValue) {
        valuesMap.put(name, newValue);
    }
    
    public Map<String,String> getChanged() {
        Map<String,String> changed = new HashMap<String,String>();
        for (String name : valuesMap.keySet()) {
            if (!valuesMap.get(name).equals(initialValuesMap.get(name))) {
                changed.put(name, valuesMap.get(name));
            }
        }
        return changed;
    }
    
    public void apply() {
        logger.info("Applying settings.");
        // begin apply
        for (Handler handler : handlers) {
            handler.beginApply();
        }
        
        // notify handlers
        Map<String,String> changed = getChanged();
        logger.info("Changed values are: " + changed);
        for (String name : changed.keySet()) {
            String oldValue = initialValuesMap.get(name);
            String newValue = changed.get(name);
            handlersMap.get(name).preferenceChanged(name, oldValue, newValue);
        }
        
        // end apply
        for (Handler handler : handlers) {
            handler.endApply();
        }
        
        // set initial values
        for (String name : valuesMap.keySet()) {
            initialValuesMap.put(name, valuesMap.get(name));
        }
    }

}
